/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 标签视图
    /// </summary>
    public partial class FCLabel : FCView {
        /// <summary>
        /// 创建标签视图
        /// </summary>
        public FCLabel() {
            setAutoSize(true);
            setBackColor(FCColor.None);
            setBorderColor(FCColor.None);
            m_size = new FCSize(100, 20);
        }

        public FCContentAlignment m_textAlign = FCContentAlignment.TopLeft;

        /// <summary>
        /// 获取文字的布局方式
        /// </summary>
        public virtual FCContentAlignment getTextAlign() {
            return m_textAlign;
        }

        /// <summary>
        /// 设置文字的布局方式
        /// </summary>
        public virtual void setTextAlign(FCContentAlignment value) {
            m_textAlign = value;
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "Label";
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            if (name == "textalign") {
                type = "enum:FCContentAlignment";
                value = FCTran.contentAlignmentToStr(getTextAlign());
            } else {
                base.getAttribute(name, ref value, ref type);
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns></returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "TextAlign" });
            return attributeNames;
        }


        /// <summary>
        /// 重绘前景方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintForeground(FCPaint paint, FCRect clipRect) {
            String text = getText();
            //绘制文字
            if (text != null && text.Length > 0) {
                int width = getWidth(), height = getHeight();
                if (width > 0 && height > 0) {
                    FCFont font = getFont();
                    FCSize tSize = paint.textSize(text, font, -1);
                    FCPoint tPoint = new FCPoint((width - tSize.cx) / 2, (height - tSize.cy) / 2);
                    FCPadding padding = getPadding(); ;
                    switch (m_textAlign) {
                        case FCContentAlignment.BottomCenter:
                            tPoint.y = height - tSize.cy;
                            break;
                        case FCContentAlignment.BottomLeft:
                            tPoint.x = padding.left;
                            tPoint.y = height - tSize.cy - padding.bottom;
                            break;
                        case FCContentAlignment.BottomRight:
                            tPoint.x = width - tSize.cx - padding.right;
                            tPoint.y = height - tSize.cy - padding.bottom;
                            break;
                        case FCContentAlignment.MiddleLeft:
                            tPoint.x = padding.left;
                            break;
                        case FCContentAlignment.MiddleRight:
                            tPoint.x = width - tSize.cx - padding.right;
                            break;
                        case FCContentAlignment.TopCenter:
                            tPoint.y = padding.top;
                            break;
                        case FCContentAlignment.TopLeft:
                            tPoint.x = padding.left;
                            tPoint.y = padding.top;
                            break;
                        case FCContentAlignment.TopRight:
                            tPoint.x = width - tSize.cx - padding.right;
                            tPoint.y = padding.top;
                            break;
                    }
                    FCRect tRect = new FCRect(tPoint.x, tPoint.y, tPoint.x + tSize.cx, tPoint.y + tSize.cy);
                    long textColor = getPaintingTextColor();
                    if (autoEllipsis() && (tRect.right > clipRect.right || tRect.bottom > clipRect.bottom)) {
                        if (tRect.right > clipRect.right) {
                            tRect.right = clipRect.right;
                        }
                        if (tRect.bottom > clipRect.bottom) {
                            tRect.bottom = clipRect.bottom;
                        }
                        paint.drawTextAutoEllipsis(text, textColor, font, tRect);
                    } else {
                        paint.drawText(text, textColor, font, tRect, -1);
                    }
                }
            }
        }

        /// <summary>
        /// 预绘图方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override bool onPrePaint(FCPaint paint, FCRect clipRect) {
            bool handle = base.onPrePaint(paint, clipRect);
            if (!handle) {
                if (autoSize()) {
                    int width = getWidth(), height = getHeight();
                    if (width > 0 && height > 0) {
                        String text = getText();
                        FCFont font = getFont();
                        FCSize tSize = paint.textSize(text, font, -1);
                        if (autoSize()) {
                            int newW = tSize.cx + 2;
                            int newH = tSize.cy + 2;
                            if (newW != width || newH != height) {
                                setSize(new FCSize(newW, newH));
                            }
                        }
                    }
                }
            }
            return handle;
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value) {
            if (name == "textalign") {
                setTextAlign(FCTran.strToContentAlignment(value));
            } else {
                base.setAttribute(name, value);
            }
        }
    }
}
